package cn.lcvc.xnc.application.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.lcvc.xnc.application.common.exception.model.MyWebException;
import cn.lcvc.xnc.application.common.utils.file.MyFileUpload;
import cn.lcvc.xnc.application.common.utils.requests.RequestUtil;
import cn.lcvc.xnc.application.dao.WasteDisposalSitePatrolMapper;
import cn.lcvc.xnc.application.dao.WasteDisposalSitePatrolPictureMapper;
import cn.lcvc.xnc.application.model.WasteDisposalMappingPicture;
import cn.lcvc.xnc.application.model.WasteDisposalSite;
import cn.lcvc.xnc.application.model.WasteDisposalSitePatrol;
import cn.lcvc.xnc.application.model.WasteDisposalSitePatrolPicture;
import cn.lcvc.xnc.application.model.query.WasteDisposalSitePatrolPictureQuery;
import cn.lcvc.xnc.application.service.WasteDisposalSitePatrolPictureService;
import cn.lcvc.xnc.authority.config.upload.FileUploadConfig;
import cn.lcvc.xnc.authority.service.impl.IBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;


/**
 * 消纳场巡逻图片服务
 *
 * @author 廖振豪
 * @date 2022/08/08
 */
@Service
@Slf4j
public class IWasteDisposalSitePatrolPictureService extends IBaseService<WasteDisposalSitePatrolPicture, WasteDisposalSitePatrolPictureQuery> implements WasteDisposalSitePatrolPictureService {

    @Autowired
    private WasteDisposalSitePatrolPictureMapper mapper;

    @Autowired
    private WasteDisposalSitePatrolMapper wasteDisposalSitePatrolMapper;

    @Autowired
    private FileUploadConfig fileUploadConfig;

    @Override
    protected void beforeGet(Serializable id) {
        if(mapper.findById(id)==null){
            throw new MyWebException("查找失败：找不到指定记录");
        }
    }

    @Override
    protected void beforeUpdate(WasteDisposalSitePatrolPicture wasteDisposalSitePatrolPicture) throws MyWebException {
        if(wasteDisposalSitePatrolPicture.getId()==null){
            throw new MyWebException("编辑失败：id不能为空");
        }
        if(mapper.findById(wasteDisposalSitePatrolPicture.getId())==null){
            throw new MyWebException("编辑失败：找不到指定记录");
        }

    }

    @Override
    protected void beforeSave(WasteDisposalSitePatrolPicture wasteDisposalSitePatrolPicture) {

    }

    @Override
    protected void beforeDelete(Serializable[] ids) {

    }


    @Override
    protected void afterGet(WasteDisposalSitePatrolPicture wasteDisposalSitePatrolPicture) {
        //添加消纳场勘察记录关联属性
        if (wasteDisposalSitePatrolPicture.getPatrolId() != null) {
            //查找勘察记录图片管理所属勘察记录
            WasteDisposalSitePatrol wasteDisposalSitePatrol = wasteDisposalSitePatrolMapper.findById(wasteDisposalSitePatrolPicture.getPatrolId());
            //赋值
            wasteDisposalSitePatrolPicture.setWasteDisposalSitePatrol(wasteDisposalSitePatrol);
        }
    }

    @Override
    protected void afterFindAll(List<WasteDisposalSitePatrolPicture> list) {
        for (WasteDisposalSitePatrolPicture wasteDisposalSitePatrolPicture : list) {
            //添加消纳场关联属性
            if (wasteDisposalSitePatrolPicture.getPatrolId() != null) {
                //查找勘察级记录管理所属消纳场
                WasteDisposalSitePatrol wasteDisposalSitePatrol = wasteDisposalSitePatrolMapper.findById(wasteDisposalSitePatrolPicture.getPatrolId());
                //赋值
                wasteDisposalSitePatrolPicture.setWasteDisposalSitePatrol(wasteDisposalSitePatrol);
            }
        }
    }

    @Override
    protected void afterSave(WasteDisposalSitePatrolPicture wasteDisposalSitePatrolPicture) {

    }

}
